package com.mlnx;

import com.mlnx.ecg.ui.panels.EcgShowType;
import com.mlnx.ecg.ui.panels.RealEcgPanel;
import com.mlnx.mptp.mptp.MpPacket;
import com.mlnx.tp.base.utils.ByteBuffUtils;
import lombok.SneakyThrows;

import javax.swing.*;
import java.awt.*;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.nio.ByteBuffer;

public class TestFrame extends JFrame {


    private JPanel contentPane;

    private Long lastTime;

    public TestFrame() {
        setIconImage(new ImageIcon("imgs/icon.png").getImage());
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setExtendedState(JFrame.MAXIMIZED_BOTH);

        contentPane = new JPanel();
        contentPane.setLayout(new BorderLayout());
        setContentPane(contentPane);

    }

    private void setPanel(JPanel panel) {
        //将panel放到指定容器中
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                contentPane.add(panel);
                contentPane.updateUI();
            }
        });
    }

    public static void main(String[] args) {
        TestFrame testFrame = new TestFrame();
        testFrame.setVisible(true);

        RealEcgPanel realEcgPanel = new RealEcgPanel(EcgShowType._1x12, 10f, 25f);

        new Thread(new Runnable() {
            @SneakyThrows
            @Override
            public void run() {
                File file = new File("D:\\aa.txt");
                System.out.println(file.exists());

                InputStream inputStream = new FileInputStream(file);

                byte[] bytes = new byte[1000];
                int len = 0;
                MpPacket mpPacket;
                ByteBuffer byteBuffer = ByteBuffer.allocate(10000000);
                byteBuffer.flip();

                TestMpDecode testMpDecode = new TestMpDecode(realEcgPanel);


                while ((len = inputStream.read(bytes)) > 0) {
                    ByteBuffUtils.addBytes(byteBuffer, bytes, len);

                    testMpDecode.decode(byteBuffer);
                }
            }
        }).start();

        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Thread.sleep(3000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                //调用心电波形绘制方法
                realEcgPanel.drawEcg();
            }
        }).start();

        testFrame.setPanel(realEcgPanel);
//        testFrame.loadHistoryEcg();
    }
}
